package com.demo.bean;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class LogAspect {

	//前置通知 joinPoint 连接点
	public void before( JoinPoint joinPoint ){
		String methodName = joinPoint.getSignature().getName();
		Object [] args = joinPoint.getArgs();
		System.out.println("开始执行.."+methodName+"...方法,参数:"+Arrays.toString(args));
	}
	//后置通知
	public void afterReturning(JoinPoint joinPoint,Object result){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("后置通知:结束执行.."+result);
	}
	
	//最终通知-出现异常继续执行
	public void after(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		Object [] args = joinPoint.getArgs();
		System.out.println("最终通知:结束执行.."+methodName+"...方法,参数:"+Arrays.toString(args));
	}
	
	//异常通知
	public void afterThrowing(JoinPoint joinPoint,Exception e){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("异常通知:"+methodName+"出现异常:"+e);
	}
	//环绕通知
	public Object around(ProceedingJoinPoint pjp){
		String m = pjp.getSignature().getName();
		Object result = null;
		System.out.println("前置通知:"+m + ",参数:"+Arrays.toString(pjp.getArgs()));
		try {
			result = pjp.proceed();
			System.out.println("后置通知:"+m+",参数:"+Arrays.toString(pjp.getArgs()));
			result = 100;
		} catch (Throwable e) {
			System.out.println("异常通知:"+m+",异常:"+e);
		}
		System.out.println("最终通知:"+m);
		return result;
	}
}
